<template>
	<my-scroll>
		<view class="p-10">
			<u-collapse :value="showMoreKeys" ref="collapse">
				<template v-for="(item,i) in ['10#润滑站','11#润滑站','12#润滑站']" :key="i">
					<u-collapse-item :name="i" :class="{'m-t-10':i>0}">
						<template #title>
							<view class="content-item-title"><text class="title-txt">{{item}}</text></view>
						</template>
						<my-grid :list="{0:list1,1:list2,2:list3}[i]" decimals="2" :col="2" />
					</u-collapse-item>
				</template>
			</u-collapse>
		</view>
	</my-scroll>
</template>

<script setup>
	import {
		getApiFabric
	} from "@/api/customApi";

	const collapse = ref(null),
		showMoreKeys = ref([0, 1, 2]),
		list = [{
			key: 'RHZ_RHBGZZT',
			title: '当前状态',
			value: false,
			unit: '',
		}, {
			key: 'RHZ_GZMS',
			title: '工作模式',
			value: '',
		}, {
			key: 'RHZ_YL',
			title: '油量',
			value: '',
			unit: 'Kg'
		}, {
			key: 'RHZ_GXYL',
			title: '管线压力',
			value: '',
			unit: 'Mpa'
		}, {
			key: 'RHZ_RHSYCSBJ',
			title: '润滑升压超时报警',
			value: false,
		}, {
			key: 'RHZ_RHBGZ',
			title: '润滑泵故障',
			value: false,
		}, {
			key: 'RHZ_CZGYLDBJ',
			title: '储脂罐油量低报警',
			value: false,
		}],
		list1 = ref([]),
		list2 = ref([]),
		list3 = ref([]);

	function fn() {
		["STM-ZNRH-RHZ10", "STM-ZNRH-RHZ11", "STM-ZNRH-RHZ12"].forEach((item, i) => {
			getApiFabric({
				apiCode: "9fcccb47-a9b7-41a9-8816-f968a54933cb",
				params: [{
					code: "deviceKeies",
					value: item,
				}],
				propertyMapping: {
					key: "name",
					val: "value"
				}
			}).then((res) => {
				const resData = ArrayToObj(res.data),
					data = [];
				cloneDeep(list).forEach(item => {
					if (resData[item.key]) {
						item.value = resData[item.key]
						data.push(item)
					}
				})
				switch (i) {
					case 0:
						list1.value = data;
						break;
					case 1:
						list2.value = data;
						break;
					case 2:
						list3.value = data;
						break;
					default:
						break;
				}
			});
		})

	}

	function getServerData(refresh) {
		Promise.all([fn()]).then(() => {
			if (refresh) {
				nextTick(() => {
					uni.stopPullDownRefresh()
				})
			}
			setTimeout(() => {
				collapse.value.init()
			}, 400)
		});
	}

	onShow(() => {
		getServerData()
	})

	onPullDownRefresh(() => {
		getServerData(true)
	})
</script>